home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / python2.4 / lib-tk / turtle.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  14.8 KB  |  553 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. from math import *
  5. import Tkinter
  6.  
  7. class Error(Exception):
  8.     pass
  9.  
  10.  
  11. class RawPen:
  12.     
  13.     def __init__(self, canvas):
  14.         self._canvas = canvas
  15.         self._items = []
  16.         self._tracing = 1
  17.         self._arrow = 0
  18.         self.degrees()
  19.         self.reset()
  20.  
  21.     
  22.     def degrees(self, fullcircle = 360.0):
  23.         self._fullcircle = fullcircle
  24.         self._invradian = pi / (fullcircle * 0.5)
  25.  
  26.     
  27.     def radians(self):
  28.         self.degrees(2.0 * pi)
  29.  
  30.     
  31.     def reset(self):
  32.         canvas = self._canvas
  33.         self._canvas.update()
  34.         width = canvas.winfo_width()
  35.         height = canvas.winfo_height()
  36.         if width <= 1:
  37.             width = canvas['width']
  38.         
  39.         if height <= 1:
  40.             height = canvas['height']
  41.         
  42.         self._origin = (float(width) / 2.0, float(height) / 2.0)
  43.         self._position = self._origin
  44.         self._angle = 0.0
  45.         self._drawing = 1
  46.         self._width = 1
  47.         self._color = 'black'
  48.         self._filling = 0
  49.         self._path = []
  50.         self._tofill = []
  51.         self.clear()
  52.         canvas._root().tkraise()
  53.  
  54.     
  55.     def clear(self):
  56.         self.fill(0)
  57.         canvas = self._canvas
  58.         items = self._items
  59.         self._items = []
  60.         for item in items:
  61.             canvas.delete(item)
  62.         
  63.         self._delete_turtle()
  64.         self._draw_turtle()
  65.  
  66.     
  67.     def tracer(self, flag):
  68.         self._tracing = flag
  69.         if not self._tracing:
  70.             self._delete_turtle()
  71.         
  72.         self._draw_turtle()
  73.  
  74.     
  75.     def forward(self, distance):
  76.         (x0, y0) = start = self._position
  77.         x1 = x0 + distance * cos(self._angle * self._invradian)
  78.         y1 = y0 - distance * sin(self._angle * self._invradian)
  79.         self._goto(x1, y1)
  80.  
  81.     
  82.     def backward(self, distance):
  83.         self.forward(-distance)
  84.  
  85.     
  86.     def left(self, angle):
  87.         self._angle = (self._angle + angle) % self._fullcircle
  88.         self._draw_turtle()
  89.  
  90.     
  91.     def right(self, angle):
  92.         self.left(-angle)
  93.  
  94.     
  95.     def up(self):
  96.         self._drawing = 0
  97.  
  98.     
  99.     def down(self):
  100.         self._drawing = 1
  101.  
  102.     
  103.     def width(self, width):
  104.         self._width = float(width)
  105.  
  106.     
  107.     def color(self, *args):
  108.         if not args:
  109.             raise Error, 'no color arguments'
  110.         
  111.         if len(args) == 1:
  112.             color = args[0]
  113.             if type(color) == type(''):
  114.                 
  115.                 try:
  116.                     id = self._canvas.create_line(0, 0, 0, 0, fill = color)
  117.                 except Tkinter.TclError:
  118.                     raise Error, 'bad color string: %r' % (color,)
  119.  
  120.                 self._set_color(color)
  121.                 return None
  122.             
  123.             
  124.             try:
  125.                 (r, g, b) = color
  126.             raise Error, 'bad color sequence: %r' % (color,)
  127.  
  128.         else:
  129.             
  130.             try:
  131.                 (r, g, b) = args
  132.             except:
  133.                 raise Error, 'bad color arguments: %r' % (args,)
  134.  
  135.         if r <= r:
  136.             pass
  137.         elif not r <= 1:
  138.             raise AssertionError
  139.         if g <= g:
  140.             pass
  141.         elif not g <= 1:
  142.             raise AssertionError
  143.         if b <= b:
  144.             pass
  145.         elif not b <= 1:
  146.             raise AssertionError
  147.         x = 255.0
  148.         y = 0.5
  149.         self._set_color('#%02x%02x%02x' % (int(r * x + y), int(g * x + y), int(b * x + y)))
  150.  
  151.     
  152.     def _set_color(self, color):
  153.         self._color = color
  154.         self._draw_turtle()
  155.  
  156.     
  157.     def write(self, arg, move = 0):
  158.         (x, y) = start = self._position
  159.         x = x - 1
  160.         item = self._canvas.create_text(x, y, text = str(arg), anchor = 'sw', fill = self._color)
  161.         self._items.append(item)
  162.         if move:
  163.             (x0, y0, x1, y1) = self._canvas.bbox(item)
  164.             self._goto(x1, y1)
  165.         
  166.         self._draw_turtle()
  167.  
  168.     
  169.     def fill(self, flag):
  170.         if self._filling:
  171.             path = tuple(self._path)
  172.             smooth = self._filling < 0
  173.             if len(path) > 2:
  174.                 item = self._canvas._create('polygon', path, {
  175.                     'fill': self._color,
  176.                     'smooth': smooth })
  177.                 self._items.append(item)
  178.                 self._canvas.lower(item)
  179.                 if self._tofill:
  180.                     for item in self._tofill:
  181.                         self._canvas.itemconfigure(item, fill = self._color)
  182.                         self._items.append(item)
  183.                     
  184.                 
  185.             
  186.         
  187.         self._path = []
  188.         self._tofill = []
  189.         self._filling = flag
  190.         if flag:
  191.             self._path.append(self._position)
  192.         
  193.         self.forward(0)
  194.  
  195.     
  196.     def circle(self, radius, extent = None):
  197.         if extent is None:
  198.             extent = self._fullcircle
  199.         
  200.         (x0, y0) = self._position
  201.         xc = x0 - radius * sin(self._angle * self._invradian)
  202.         yc = y0 - radius * cos(self._angle * self._invradian)
  203.         if radius >= 0.0:
  204.             start = self._angle - 90.0
  205.         else:
  206.             start = self._angle + 90.0
  207.             extent = -extent
  208.         if self._filling:
  209.             if abs(extent) >= self._fullcircle:
  210.                 item = self._canvas.create_oval(xc - radius, yc - radius, xc + radius, yc + radius, width = self._width, outline = '')
  211.                 self._tofill.append(item)
  212.             
  213.             item = self._canvas.create_arc(xc - radius, yc - radius, xc + radius, yc + radius, style = 'chord', start = start, extent = extent, width = self._width, outline = '')
  214.             self._tofill.append(item)
  215.         
  216.         if self._drawing:
  217.             if abs(extent) >= self._fullcircle:
  218.                 item = self._canvas.create_oval(xc - radius, yc - radius, xc + radius, yc + radius, width = self._width, outline = self._color)
  219.                 self._items.append(item)
  220.             
  221.             item = self._canvas.create_arc(xc - radius, yc - radius, xc + radius, yc + radius, style = 'arc', start = start, extent = extent, width = self._width, outline = self._color)
  222.             self._items.append(item)
  223.         
  224.         angle = start + extent
  225.         x1 = xc + abs(radius) * cos(angle * self._invradian)
  226.         y1 = yc - abs(radius) * sin(angle * self._invradian)
  227.         self._angle = (self._angle + extent) % self._fullcircle
  228.         self._position = (x1, y1)
  229.         if self._filling:
  230.             self._path.append(self._position)
  231.         
  232.         self._draw_turtle()
  233.  
  234.     
  235.     def heading(self):
  236.         return self._angle
  237.  
  238.     
  239.     def setheading(self, angle):
  240.         self._angle = angle
  241.         self._draw_turtle()
  242.  
  243.     
  244.     def window_width(self):
  245.         width = self._canvas.winfo_width()
  246.         if width <= 1:
  247.             width = self._canvas['width']
  248.         
  249.         return width
  250.  
  251.     
  252.     def window_height(self):
  253.         height = self._canvas.winfo_height()
  254.         if height <= 1:
  255.             height = self._canvas['height']
  256.         
  257.         return height
  258.  
  259.     
  260.     def position(self):
  261.         (x0, y0) = self._origin
  262.         (x1, y1) = self._position
  263.         return [
  264.             x1 - x0,
  265.             -y1 + y0]
  266.  
  267.     
  268.     def setx(self, xpos):
  269.         (x0, y0) = self._origin
  270.         (x1, y1) = self._position
  271.         self._goto(x0 + xpos, y1)
  272.  
  273.     
  274.     def sety(self, ypos):
  275.         (x0, y0) = self._origin
  276.         (x1, y1) = self._position
  277.         self._goto(x1, y0 - ypos)
  278.  
  279.     
  280.     def goto(self, *args):
  281.         if len(args) == 1:
  282.             
  283.             try:
  284.                 (x, y) = args[0]
  285.             raise Error, 'bad point argument: %r' % (args[0],)
  286.  
  287.         else:
  288.             
  289.             try:
  290.                 (x, y) = args
  291.             except:
  292.                 raise Error, 'bad coordinates: %r' % (args[0],)
  293.  
  294.         (x0, y0) = self._origin
  295.         self._goto(x0 + x, y0 - y)
  296.  
  297.     
  298.     def _goto(self, x1, y1):
  299.         (x0, y0) = start = self._position
  300.         self._position = map(float, (x1, y1))
  301.         if self._filling:
  302.             self._path.append(self._position)
  303.         
  304.         if self._drawing:
  305.             if self._tracing:
  306.                 dx = float(x1 - x0)
  307.                 dy = float(y1 - y0)
  308.                 distance = hypot(dx, dy)
  309.                 nhops = int(distance)
  310.                 item = self._canvas.create_line(x0, y0, x0, y0, width = self._width, capstyle = 'round', fill = self._color)
  311.                 
  312.                 try:
  313.                     for i in range(1, 1 + nhops):
  314.                         x = x0 + dx * i / nhops
  315.                         y = y0 + dy * i / nhops
  316.                         self._canvas.coords(item, x0, y0, x, y)
  317.                         self._draw_turtle((x, y))
  318.                         self._canvas.update()
  319.                         self._canvas.after(10)
  320.                     
  321.                     self._canvas.coords(item, x0, y0, x1, y1)
  322.                     self._canvas.itemconfigure(item, arrow = 'none')
  323.                 except Tkinter.TclError:
  324.                     return None
  325.                 except:
  326.                     None<EXCEPTION MATCH>Tkinter.TclError
  327.                 
  328.  
  329.             None<EXCEPTION MATCH>Tkinter.TclError
  330.             item = self._canvas.create_line(x0, y0, x1, y1, width = self._width, capstyle = 'round', fill = self._color)
  331.             self._items.append(item)
  332.         
  333.         self._draw_turtle()
  334.  
  335.     
  336.     def _draw_turtle(self, position = []):
  337.         if not self._tracing:
  338.             return None
  339.         
  340.         if position == []:
  341.             position = self._position
  342.         
  343.         (x, y) = position
  344.         distance = 8
  345.         dx = distance * cos(self._angle * self._invradian)
  346.         dy = distance * sin(self._angle * self._invradian)
  347.         self._delete_turtle()
  348.         self._arrow = self._canvas.create_line(x - dx, y + dy, x, y, width = self._width, arrow = 'last', capstyle = 'round', fill = self._color)
  349.         self._canvas.update()
  350.  
  351.     
  352.     def _delete_turtle(self):
  353.         if self._arrow != 0:
  354.             self._canvas.delete(self._arrow)
  355.         
  356.         self._arrow = 0
  357.  
  358.  
  359. _root = None
  360. _canvas = None
  361. _pen = None
  362.  
  363. class Pen(RawPen):
  364.     
  365.     def __init__(self):
  366.         global _root, _canvas
  367.         if _root is None:
  368.             _root = Tkinter.Tk()
  369.             _root.wm_protocol('WM_DELETE_WINDOW', self._destroy)
  370.         
  371.         if _canvas is None:
  372.             _canvas = Tkinter.Canvas(_root, background = 'white')
  373.             _canvas.pack(expand = 1, fill = 'both')
  374.         
  375.         RawPen.__init__(self, _canvas)
  376.  
  377.     
  378.     def _destroy(self):
  379.         global _pen, _root, _canvas
  380.         root = self._canvas._root()
  381.         if root is _root:
  382.             _pen = None
  383.             _root = None
  384.             _canvas = None
  385.         
  386.         root.destroy()
  387.  
  388.  
  389.  
  390. def _getpen():
  391.     global _pen
  392.     pen = _pen
  393.     if not pen:
  394.         _pen = pen = Pen()
  395.     
  396.     return pen
  397.  
  398.  
  399. def degrees():
  400.     _getpen().degrees()
  401.  
  402.  
  403. def radians():
  404.     _getpen().radians()
  405.  
  406.  
  407. def reset():
  408.     _getpen().reset()
  409.  
  410.  
  411. def clear():
  412.     _getpen().clear()
  413.  
  414.  
  415. def tracer(flag):
  416.     _getpen().tracer(flag)
  417.  
  418.  
  419. def forward(distance):
  420.     _getpen().forward(distance)
  421.  
  422.  
  423. def backward(distance):
  424.     _getpen().backward(distance)
  425.  
  426.  
  427. def left(angle):
  428.     _getpen().left(angle)
  429.  
  430.  
  431. def right(angle):
  432.     _getpen().right(angle)
  433.  
  434.  
  435. def up():
  436.     _getpen().up()
  437.  
  438.  
  439. def down():
  440.     _getpen().down()
  441.  
  442.  
  443. def width(width):
  444.     _getpen().width(width)
  445.  
  446.  
  447. def color(*args):
  448.     _getpen().color(*args)
  449.  
  450.  
  451. def write(arg, move = 0):
  452.     _getpen().write(arg, move)
  453.  
  454.  
  455. def fill(flag):
  456.     _getpen().fill(flag)
  457.  
  458.  
  459. def circle(radius, extent = None):
  460.     _getpen().circle(radius, extent)
  461.  
  462.  
  463. def goto(*args):
  464.     _getpen().goto(*args)
  465.  
  466.  
  467. def heading():
  468.     return _getpen().heading()
  469.  
  470.  
  471. def setheading(angle):
  472.     _getpen().setheading(angle)
  473.  
  474.  
  475. def position():
  476.     return _getpen().position()
  477.  
  478.  
  479. def window_width():
  480.     return _getpen().window_width()
  481.  
  482.  
  483. def window_height():
  484.     return _getpen().window_height()
  485.  
  486.  
  487. def setx(xpos):
  488.     _getpen().setx(xpos)
  489.  
  490.  
  491. def sety(ypos):
  492.     _getpen().sety(ypos)
  493.  
  494.  
  495. def demo():
  496.     reset()
  497.     tracer(1)
  498.     up()
  499.     backward(100)
  500.     down()
  501.     width(3)
  502.     for i in range(3):
  503.         if i == 2:
  504.             fill(1)
  505.         
  506.         for j in range(4):
  507.             forward(20)
  508.             left(90)
  509.         
  510.         if i == 2:
  511.             color('maroon')
  512.             fill(0)
  513.         
  514.         up()
  515.         forward(30)
  516.         down()
  517.     
  518.     width(1)
  519.     color('black')
  520.     tracer(0)
  521.     up()
  522.     right(90)
  523.     forward(100)
  524.     right(90)
  525.     forward(100)
  526.     right(180)
  527.     down()
  528.     write('startstart', 1)
  529.     write('start', 1)
  530.     color('red')
  531.     for i in range(5):
  532.         forward(20)
  533.         left(90)
  534.         forward(20)
  535.         right(90)
  536.     
  537.     fill(1)
  538.     for i in range(5):
  539.         forward(20)
  540.         left(90)
  541.         forward(20)
  542.         right(90)
  543.     
  544.     fill(0)
  545.     write('end')
  546.     if __name__ == '__main__':
  547.         _root.mainloop()
  548.     
  549.  
  550. if __name__ == '__main__':
  551.     demo()
  552.  
  553.